package com.usian.article.listener;

import com.usian.article.service.IApHotArticlesService;
import com.usian.common.constants.message.HotArticleConstants;
import com.usian.common.util.JsonUtils;
import com.usian.model.article.msg.ArticleBehaviorStreamMsg;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 监听热点文章行为消息
 */
@Component
public class HotArticleScoreConsumerListener {

    @Autowired
    private IApHotArticlesService apHotArticlesService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_SCORE_CONSUMER_TOPIC)
    public void receiveMsg(ConsumerRecord<String,String> record){
        //判断消息内容是否存在
        Optional<ConsumerRecord<String, String>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            //获取消息
            String value = record.value();
            ArticleBehaviorStreamMsg articleBehaviorStreamMsg = JsonUtils.toBean(value, ArticleBehaviorStreamMsg.class);
            //更新热点文章分值
            apHotArticlesService.updateHotArticle(articleBehaviorStreamMsg);
        }
    }
}
